package simple.utils.pool;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created with IntelliJ IDEA.
 * User: rehak
 * Date: 5.4.13
 * Time: 14:26
 * To change this template use File | Settings | File Templates.
 */
public class PoolFactory {

    private PoolFactory() {
        throw new IllegalArgumentException("Not instancianable!");
    }

    public static Pool<MessageDigest> messageDigestPool(final String algorithm) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md = null;
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Unsupported algorithm " + algorithm, e);
        }
        return new Pool<MessageDigest>() {
            @Override
            protected MessageDigest createInstance() {
                try {
                    return MessageDigest.getInstance(algorithm);
                } catch (NoSuchAlgorithmException e) {
                    throw new IllegalArgumentException("Unsupported algorithm " + algorithm, e);
                }
            }

            @Override
            protected void cleanUp(MessageDigest instance) {
                if (instance != null) {
                    instance.reset();
                }
            }
        };
    }

}
